import { defineStore } from 'pinia'
import { getRoleList, getRoleDetail, createRole, updateRole, deleteRole, getPermissionList, updateRolePermissions } from '@/api/role'

export const useRoleStore = defineStore('role', {
  state: () => ({
    roleList: [],
    roleDetail: {},
    permissionList: [],
    loading: false,
    total: 0
  }),

  actions: {
    // 获取角色列表
    async fetchRoleList(params) {
      this.loading = true
      try {
        const response = await getRoleList(params)
        this.roleList = response.data
        this.total = response.total
        return response
      } catch (error) {
        console.error('获取角色列表失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 获取角色详情
    async fetchRoleDetail(id) {
      this.loading = true
      try {
        const response = await getRoleDetail(id)
        this.roleDetail = response.data
        return response
      } catch (error) {
        console.error('获取角色详情失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 创建角色
    async createRole(data) {
      try {
        const response = await createRole(data)
        // 如果当前在角色列表页，重新获取列表
        if (this.roleList.length > 0) {
          await this.fetchRoleList({})
        }
        return response
      } catch (error) {
        console.error('创建角色失败:', error)
        throw error
      }
    },

    // 更新角色
    async updateRole(id, data) {
      try {
        const response = await updateRole(id, data)
        // 更新角色列表中的数据
        const index = this.roleList.findIndex(role => role.id === id)
        if (index !== -1) {
          this.roleList[index] = { ...this.roleList[index], ...data }
        }
        // 如果是当前详情页的角色，也更新详情数据
        if (this.roleDetail.id === id) {
          this.roleDetail = { ...this.roleDetail, ...data }
        }
        return response
      } catch (error) {
        console.error('更新角色失败:', error)
        throw error
      }
    },

    // 删除角色
    async deleteRole(id) {
      try {
        const response = await deleteRole(id)
        // 从角色列表中移除
        this.roleList = this.roleList.filter(role => role.id !== id)
        this.total--
        return response
      } catch (error) {
        console.error('删除角色失败:', error)
        throw error
      }
    },

    // 获取权限列表
    async fetchPermissionList() {
      this.loading = true
      try {
        const response = await getPermissionList()
        this.permissionList = response.data
        return response
      } catch (error) {
        console.error('获取权限列表失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 更新角色权限
    async updateRolePermissions(roleId, permissions) {
      try {
        const response = await updateRolePermissions(roleId, permissions)
        // 如果是当前详情页的角色，也更新详情数据
        if (this.roleDetail.id === roleId) {
          this.roleDetail.permissions = permissions
        }
        return response
      } catch (error) {
        console.error('更新角色权限失败:', error)
        throw error
      }
    }
  }
})